import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:management/model/ask_for_leave_model.dart';

///请假记录
class AskForLeave extends StatefulWidget {
  @override
  _AskForLeaveState createState() => _AskForLeaveState();
}

class _AskForLeaveState extends State<AskForLeave> {
  final ScrollController _controllerOne = ScrollController();
  final TextEditingController _nameController = TextEditingController();
  List<AskForLeaveModel> askForLeaveRecord = [];
  var _rowsPerPage = 10;

  @override
  void initState() {
    super.initState();
    askForLeaveRecord = _requestData();
  }

  ///搜索栏
  Widget _buildSearchBar() {
    return Container(
      margin: EdgeInsets.only(top: 20, left: 20),
      height: 60,
      child: Row(
        children: [
          Container(
            width: 200,
            child: TextField(
              controller: _nameController,
              decoration:
                  InputDecoration(border: OutlineInputBorder(), hintText: "姓名"),
            ),
          ),
          Container(
            width: 150,
            height: 60,
            margin: EdgeInsets.only(left: 30),
            child: MaterialButton(
              color: Colors.blue,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Icon(
                    Icons.search,
                    color: Colors.white,
                  ),
                  Text(
                    "搜索",
                    style: TextStyle(color: Colors.white, fontSize: 20),
                  ),
                ],
              ),
              onPressed: () {
                var name = _nameController.text;
                if (TextUtil.isEmpty(name)) {
                  askForLeaveRecord = _requestData();
                } else {
                  var requestData = _requestData();
                  askForLeaveRecord = [];
                  requestData.forEach((element) {
                    if (element.leavingStudent == name) {
                      askForLeaveRecord.add(element);
                    }
                  });
                }
                setState(() {});
              },
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(
          "请假记录",
          style: TextStyle(fontSize: 30),
        ),
        backgroundColor: Colors.green,
      ),
      body: _buildBody(),
    );
  }

  Widget _buildBody() => Container(
        //日期 请假学生 班级 请假原因 请假时间  值班人 备注
        height: MediaQuery.of(context).size.height,
        child: Column(
          children: [
            _buildSearchBar(),
            // _buildTableHeader(),
            Expanded(
              // child: _buildTableContent(),
              child: _buildAskForLeaveTable(),
            )
          ],
        ),
      );

  ///模拟数据
  List<AskForLeaveModel> _requestData() {
    return [
      AskForLeaveModel.formMap({
        "leavingStudent": "张三",
        "className": "高一1",
        "reasonForLeave": "病假",
        "totalLeaveDays": "2天",
        "leaveStartDate": "2021-07-24",
        "leaveDeadline": "2021-07-26"
      }),
      AskForLeaveModel.formMap({
        "leavingStudent": "李四",
        "className": "七1",
        "reasonForLeave": "事假",
        "totalLeaveDays": "1天",
        "leaveStartDate": "2021-07-24",
        "leaveDeadline": "2021-07-25"
      }),
      AskForLeaveModel.formMap({
        "leavingStudent": "王五",
        "className": "高一1",
        "reasonForLeave": "病假",
        "totalLeaveDays": "2天",
        "leaveStartDate": "2021-07-24",
        "leaveDeadline": "2021-07-26"
      }),
      AskForLeaveModel.formMap({
        "leavingStudent": "李雷",
        "className": "七1",
        "reasonForLeave": "事假",
        "totalLeaveDays": "1天",
        "leaveStartDate": "2021-07-24",
        "leaveDeadline": "2021-07-25"
      }),
      AskForLeaveModel.formMap({
        "leavingStudent": "张三",
        "className": "高一1",
        "reasonForLeave": "病假",
        "totalLeaveDays": "2天",
        "leaveStartDate": "2021-07-24",
        "leaveDeadline": "2021-07-26"
      }),
      AskForLeaveModel.formMap({
        "leavingStudent": "李四",
        "className": "七1",
        "reasonForLeave": "事假",
        "totalLeaveDays": "1天",
        "leaveStartDate": "2021-07-24",
        "leaveDeadline": "2021-07-25"
      }),
      AskForLeaveModel.formMap({
        "leavingStudent": "张三",
        "className": "高一1",
        "reasonForLeave": "病假",
        "totalLeaveDays": "2天",
        "leaveStartDate": "2021-07-24",
        "leaveDeadline": "2021-07-26"
      }),
      AskForLeaveModel.formMap({
        "leavingStudent": "李四",
        "className": "七1",
        "reasonForLeave": "事假",
        "totalLeaveDays": "1天",
        "leaveStartDate": "2021-07-24",
        "leaveDeadline": "2021-07-25"
      }),
      AskForLeaveModel.formMap({
        "leavingStudent": "张三",
        "className": "高一1",
        "reasonForLeave": "病假",
        "totalLeaveDays": "2天",
        "leaveStartDate": "2021-07-24",
        "leaveDeadline": "2021-07-26"
      }),
      AskForLeaveModel.formMap({
        "leavingStudent": "李四",
        "className": "七1",
        "reasonForLeave": "事假",
        "totalLeaveDays": "1天",
        "leaveStartDate": "2021-07-24",
        "leaveDeadline": "2021-07-25"
      }),
      AskForLeaveModel.formMap({
        "leavingStudent": "张三",
        "className": "高一1",
        "reasonForLeave": "病假",
        "totalLeaveDays": "2天",
        "leaveStartDate": "2021-07-24",
        "leaveDeadline": "2021-07-26"
      }),
      AskForLeaveModel.formMap({
        "leavingStudent": "李四",
        "className": "七1",
        "reasonForLeave": "事假",
        "totalLeaveDays": "1天",
        "leaveStartDate": "2021-07-24",
        "leaveDeadline": "2021-07-25"
      }),
      AskForLeaveModel.formMap({
        "leavingStudent": "张三",
        "className": "高一1",
        "reasonForLeave": "病假",
        "totalLeaveDays": "2天",
        "leaveStartDate": "2021-07-24",
        "leaveDeadline": "2021-07-26"
      }),
      AskForLeaveModel.formMap({
        "leavingStudent": "李四",
        "className": "七1",
        "reasonForLeave": "事假",
        "totalLeaveDays": "1天",
        "leaveStartDate": "2021-07-24",
        "leaveDeadline": "2021-07-25"
      }),
      AskForLeaveModel.formMap({
        "leavingStudent": "张三",
        "className": "高一1",
        "reasonForLeave": "病假",
        "totalLeaveDays": "2天",
        "leaveStartDate": "2021-07-24",
        "leaveDeadline": "2021-07-26"
      }),
      AskForLeaveModel.formMap({
        "leavingStudent": "李四",
        "className": "七1",
        "reasonForLeave": "事假",
        "totalLeaveDays": "1天",
        "leaveStartDate": "2021-07-24",
        "leaveDeadline": "2021-07-25"
      }),
    ];
  }

  Widget _buildItem(BuildContext context, int position) {
    var askForLeaveModel = askForLeaveRecord[position];

    return Container(
      // width: 1250,
      height: 60,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            height: 60,
            width: 300,
            decoration: BoxDecoration(
              color: position.isOdd ? Colors.grey[300] : Colors.white,
              shape: BoxShape.rectangle,
              border: Border.all(),
            ),
            child: Text(
              askForLeaveModel.leavingStudent!,
              style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25),
            ),
            alignment: Alignment.center,
          ),
          Container(
            height: 60,
            width: 300,
            decoration: BoxDecoration(
              color: position.isOdd ? Colors.grey[300] : Colors.white,
              shape: BoxShape.rectangle,
              border: Border.all(),
            ),
            child: Text(askForLeaveModel.className!,
                style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)),
            alignment: Alignment.center,
          ),
          Container(
            height: 60,
            width: 300,
            decoration: BoxDecoration(
              color: position.isOdd ? Colors.grey[300] : Colors.white,
              shape: BoxShape.rectangle,
              border: Border.all(),
            ),
            child: Text(askForLeaveModel.reasonForLeave!,
                style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)),
            alignment: Alignment.center,
          ),
          Container(
            height: 60,
            width: 300,
            decoration: BoxDecoration(
              color: position.isOdd ? Colors.grey[300] : Colors.white,
              shape: BoxShape.rectangle,
              border: Border.all(),
            ),
            child: Text(
              askForLeaveModel.leaveStartDate!,
              style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25),
            ),
            alignment: Alignment.center,
          ),
          Container(
            height: 60,
            width: 300,
            decoration: BoxDecoration(
              color: position.isOdd ? Colors.grey[300] : Colors.white,
              shape: BoxShape.rectangle,
              border: Border.all(),
            ),
            child: Text(
              askForLeaveModel.leaveDeadline!,
              style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25),
            ),
            alignment: Alignment.center,
          ),
          Container(
            height: 60,
            width: 300,
            decoration: BoxDecoration(
              color: position.isOdd ? Colors.grey[300] : Colors.white,
              shape: BoxShape.rectangle,
              border: Border.all(),
            ),
            child: Text(
              askForLeaveModel.totalLeaveDays!,
              style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25),
            ),
            alignment: Alignment.center,
          ),
        ],
      ),
    );
  }

  ///请假记录表头
  Widget _buildTableHeader() {
    return Container(
      padding: EdgeInsets.only(top: 20, left: 20, right: 20),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            height: 60,
            width: 300,
            decoration: BoxDecoration(
              color: Colors.grey,
              shape: BoxShape.rectangle,
              border: Border.all(),
            ),
            child: Text(
              "请假学生",
              style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25),
            ),
            alignment: Alignment.center,
          ),
          Container(
            height: 60,
            width: 300,
            decoration: BoxDecoration(
              color: Colors.grey,
              shape: BoxShape.rectangle,
              border: Border.all(),
            ),
            child: Text("班级",
                style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)),
            alignment: Alignment.center,
          ),
          Container(
            height: 60,
            width: 300,
            decoration: BoxDecoration(
              color: Colors.grey,
              shape: BoxShape.rectangle,
              border: Border.all(),
            ),
            child: Text("请假原因",
                style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)),
            alignment: Alignment.center,
          ),
          Container(
            height: 60,
            width: 300,
            decoration: BoxDecoration(
              color: Colors.grey,
              shape: BoxShape.rectangle,
              border: Border.all(),
            ),
            child: Text("请假开始日期",
                style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)),
            alignment: Alignment.center,
          ),
          Container(
            height: 60,
            width: 300,
            decoration: BoxDecoration(
              color: Colors.grey,
              shape: BoxShape.rectangle,
              border: Border.all(),
            ),
            child: Text("请假截止日期",
                style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)),
            alignment: Alignment.center,
          ),
          Container(
            height: 60,
            width: 300,
            decoration: BoxDecoration(
              color: Colors.grey,
              shape: BoxShape.rectangle,
              border: Border.all(),
            ),
            child: Text("合计请假天数",
                style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)),
            alignment: Alignment.center,
          ),
        ],
      ),
    );
  }

  ///构建表内容
  Widget _buildTableContent() {
    return Scrollbar(
      thickness: 15,
      controller: _controllerOne,
      isAlwaysShown: false,
      child: ListView.builder(
        controller: _controllerOne,
        padding: EdgeInsets.symmetric(horizontal: 20),
        itemBuilder: (context, position) => _buildItem(context, position),
        itemCount: askForLeaveRecord.length,
      ),
    );
  }

  Widget _buildAskForLeaveTable() {
    return SingleChildScrollView(
      child: Container(
        child: PaginatedDataTable(
          header: Text(""),
          dataRowHeight: 60,
          columnSpacing: 0,
          horizontalMargin: (MediaQuery.of(context).size.width - 1800) / 2,
          availableRowsPerPage: [5, 10, 15, 20, 30],
          rowsPerPage: _rowsPerPage,
          onRowsPerPageChanged: (v) {
            setState(() {
              _rowsPerPage = v!;
            });
          },
          columns: [
            DataColumn(
              label: Container(
                height: 60,
                width: 300,
                padding: EdgeInsets.zero,
                decoration: BoxDecoration(
                  color: Colors.grey,
                  shape: BoxShape.rectangle,
                  // border: Border(right: BorderSide(),bottom: BorderSide(width: 2)),
                ),
                child: Text(
                  "请假学生",
                  style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25),
                ),
                alignment: Alignment.center,
              ),
            ),
            DataColumn(
              label: Container(
                height: 60,
                width: 300,
                padding: EdgeInsets.zero,
                decoration: BoxDecoration(
                  color: Colors.grey,
                  shape: BoxShape.rectangle,
                  // border: Border(left: BorderSide(),right: BorderSide(),bottom: BorderSide(width: 2)),
                ),
                child: Text("班级",
                    style:
                        TextStyle(fontWeight: FontWeight.bold, fontSize: 25)),
                alignment: Alignment.center,
              ),
            ),
            DataColumn(
              label: Container(
                height: 60,
                width: 300,
                padding: EdgeInsets.zero,
                decoration: BoxDecoration(
                  color: Colors.grey,
                  shape: BoxShape.rectangle,
                  // border: Border(left: BorderSide(),right: BorderSide(),bottom: BorderSide(width: 2)),
                ),
                child: Text("请假原因",
                    style:
                        TextStyle(fontWeight: FontWeight.bold, fontSize: 25)),
                alignment: Alignment.center,
              ),
            ),
            DataColumn(
              label: Container(
                height: 60,
                width: 300,
                padding: EdgeInsets.zero,
                decoration: BoxDecoration(
                  color: Colors.grey,
                  shape: BoxShape.rectangle,
                  // border: Border(left: BorderSide(),right: BorderSide(),bottom: BorderSide(width: 2)),
                ),
                child: Text("请假开始日期",
                    style:
                        TextStyle(fontWeight: FontWeight.bold, fontSize: 25)),
                alignment: Alignment.center,
              ),
            ),
            DataColumn(
              label: Container(
                height: 60,
                width: 300,
                padding: EdgeInsets.zero,
                decoration: BoxDecoration(
                  color: Colors.grey,
                  shape: BoxShape.rectangle,
                  // border: Border(left: BorderSide(),right: BorderSide(),bottom: BorderSide(width: 2)),
                ),
                child: Text("请假截止日期",
                    style:
                        TextStyle(fontWeight: FontWeight.bold, fontSize: 25)),
                alignment: Alignment.center,
              ),
            ),
            DataColumn(
              label: Container(
                height: 60,
                width: 300,
                padding: EdgeInsets.zero,
                decoration: BoxDecoration(
                  color: Colors.grey,
                  shape: BoxShape.rectangle,
                  // border: Border(left: BorderSide(),bottom: BorderSide(width: 2)),
                ),
                child: Text("合计请假天数",
                    style:
                        TextStyle(fontWeight: FontWeight.bold, fontSize: 25)),
                alignment: Alignment.center,
              ),
            ),
          ],
          source: AskForLeaveDataTableSource(askForLeaveRecord),
        ),
      ),
    );
  }
}

class AskForLeaveDataTableSource extends DataTableSource {
  final List<AskForLeaveModel> data;

  AskForLeaveDataTableSource(this.data);

  @override
  DataRow? getRow(int index) {
    if (index >= data.length) {
      return null;
    }
    return DataRow.byIndex(
      index: index,
      cells: [
        DataCell(Container(
          height: 60,
          width: 300,
          decoration: BoxDecoration(
            color: index.isOdd ? Colors.grey[300] : Colors.white,
            shape: BoxShape.rectangle,
            border: Border(
                top: BorderSide(color: Colors.grey[300]!),
                bottom: BorderSide(color: Colors.grey[300]!)),
          ),
          child: Text(
            data[index].leavingStudent!,
            style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25),
          ),
          alignment: Alignment.center,
        )),
        DataCell(
          Container(
            height: 60,
            width: 300,
            decoration: BoxDecoration(
              color: index.isOdd ? Colors.grey[300] : Colors.white,
              shape: BoxShape.rectangle,
              border: Border(
                  top: BorderSide(color: Colors.grey[300]!),
                  bottom: BorderSide(color: Colors.grey[300]!)),
            ),
            child: Text(data[index].className!,
                style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)),
            alignment: Alignment.center,
          ),
        ),
        DataCell(
          Container(
            height: 60,
            width: 300,
            decoration: BoxDecoration(
              color: index.isOdd ? Colors.grey[300] : Colors.white,
              shape: BoxShape.rectangle,
              border: Border(
                  top: BorderSide(color: Colors.grey[300]!),
                  bottom: BorderSide(color: Colors.grey[300]!)),
            ),
            child: Text(data[index].reasonForLeave!,
                style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)),
            alignment: Alignment.center,
          ),
        ),
        DataCell(
          Container(
            height: 60,
            width: 300,
            decoration: BoxDecoration(
              color: index.isOdd ? Colors.grey[300] : Colors.white,
              shape: BoxShape.rectangle,
              border: Border(
                  top: BorderSide(color: Colors.grey[300]!),
                  bottom: BorderSide(color: Colors.grey[300]!)),
            ),
            child: Text(
              data[index].leaveStartDate!,
              style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25),
            ),
            alignment: Alignment.center,
          ),
        ),
        DataCell(
          Container(
            height: 60,
            width: 300,
            decoration: BoxDecoration(
              color: index.isOdd ? Colors.grey[300] : Colors.white,
              shape: BoxShape.rectangle,
              border: Border(
                  top: BorderSide(color: Colors.grey[300]!),
                  bottom: BorderSide(color: Colors.grey[300]!)),
            ),
            child: Text(
              data[index].leaveDeadline!,
              style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25),
            ),
            alignment: Alignment.center,
          ),
        ),
        DataCell(
          Container(
            height: 60,
            width: 300,
            decoration: BoxDecoration(
              color: index.isOdd ? Colors.grey[300] : Colors.white,
              shape: BoxShape.rectangle,
              border: Border(
                  top: BorderSide(color: Colors.grey[300]!),
                  bottom: BorderSide(color: Colors.grey[300]!)),
            ),
            child: Text(
              data[index].totalLeaveDays!,
              style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25),
            ),
            alignment: Alignment.center,
          ),
        ),
      ],
    );
  }

  @override
  bool get isRowCountApproximate => false;

  @override
  int get rowCount => data.length;

  @override
  int get selectedRowCount => 0;
}
